package com.example.shardingspheredemo.algorithm.standard;


import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.math.BigInteger;
import java.util.Collection;

/**
 * 表的精确查询分片
 */
public class MyPreciseTableShardingAlgorithm implements PreciseShardingAlgorithm<Long> {

	@Override
	public String doSharding(Collection collection, PreciseShardingValue preciseShardingValue) {

		String logicTableName = preciseShardingValue.getLogicTableName();
		String id = preciseShardingValue.getColumnName();
		BigInteger idValue = new BigInteger(String.valueOf((Long) preciseShardingValue.getValue()));
		String key = logicTableName + "_" + (idValue.mod(new BigInteger("2")).add(new BigInteger("1")));
		if (collection.contains(key)){
			return key;
		}
		throw new UnsupportedOperationException("key is not exits");
	}
}
